package com.itcast.demo.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("top")
public class ShowTopController {
    private Object lock1 = new Object();
    private Object lock2 = new Object();

    @RequestMapping("test")
    public String test() {
        return "success";
    }
    @RequestMapping("loop")
    public String loop() {
        System.out.println("start");
        // 测试死循环
         // 测试内存溢出
        boolean flag = true;
        while (flag){
            System.out.println("loop--------------");
        }
        return "test===Hello World！！！";
    }
    @GetMapping("deadLock")
    public String deadLock() {
        new Thread(() -> {
            synchronized (lock1) {
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (lock2) {
                    System.out.println(" thread1 over! ");
                }
            }
        }).start();
        new Thread(() -> {
            synchronized (lock2) {
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (lock1) {
                    System.out.println(" thread2 over! ");
                }
            }
        }).start();
        return "success";

    }

}
